//地图中的最高点
class Solution {
    int m, n;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    typedef pair<int, int> PII;

public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        m = isWater.size();
        n = isWater[0].size();
        //  vector<vector<bool>> vis(m, vector<bool>(n));
        queue<PII> q;
        vector<vector<int>> ret(m, vector<int>(n, -1));
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (isWater[i][j] == 1) {
                    q.push({i, j});
                    // vis[i][j] = true;
                    ret[i][j] = 0;
                }
        while (q.size()) {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && ret[x][y] == -1) {
                    ret[x][y] = ret[a][b] + 1;
                    q.push({x, y});
                }
            }
        }
        return ret;
    }
};

//地图分析
class Solution {
    int m, n;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    typedef pair<int, int> PII;

public:
    int maxDistance(vector<vector<int>>& grid) {
        m = grid.size();
        n = grid[0].size();
        int _ret = -1;
        queue<PII> q;
        vector<vector<int>> ret(m, vector<int>(n, -1));
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (grid[i][j] == 1) {
                    q.push({i, j});
                    // vis[i][j] = true;
                    ret[i][j] = 0;
                }
        while (q.size()) {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && ret[x][y] == -1) {
                    ret[x][y] = ret[a][b] + 1;
                    _ret = max(ret[x][y], _ret);
                    q.push({x, y});
                }
            }
        }
        return _ret;
    }
};